#!/usr/bin/python
# -*- coding: utf-8 -*-

__all__ = [
           u'Vector',
           u'StrList',
           u'pyLotList'
           ]

import cStringIO

from _pylot.data.fileformat import DialectpyLotList

from _pylot.core.utils import (list_to_unicode_list, check_ascii, error)
from _pylot.core.system import ENCODING
from _pylot.core.csvtools import UnicodeWriter, unicode_csv_reader

class DatumTypeSample (object):
  def __init__(self, value):
    u"""
    Exactly ONE argument required. Default value allowed.
    """
    pass

  @classmethod
  def FromCSV (cls, src):
    value = cls()
    value.fromCSVValue(src)
    return value

  @classmethod
  def FrompyLot (cls, src):
    value = cls()
    value.fromCSVValue(src)
    return value

  def upFrompyLot (self, src):
    pass

  def upFromCSV (self, src):
    pass

  def topyLot (self, src):
    pass

  def toCSV (self, src):
    pass

class BaseList (list):

  def __init__(self, value=[]):
    new_list = self.convertList(value)
    list.__init__(self, new_list)

  def __repr__ (self):
    return '%s(%s)' % (self.__class__.__name__, list.__repr__(self))

  def __getslice__(self, b, c):
    return self.__class__(list.__getslice__(self, b, c))

  @classmethod
  def convertList (cls, src):
    new_list = []
    for val in src :
      new_list.append(cls.convertValue(val))
    return new_list

  @classmethod
  def convertValue(cls, src):
    return src

  @classmethod
  def FromCSV (cls, src):
    src = src.strip()
    src = src.rstrip(u']')
    src = src.lstrip(u'[')

    lst_reader = unicode_csv_reader([src], dialect=DialectpyLotList())
    for row in lst_reader :
      csvlst = [field.strip() for field in row]
    csvlst = cls.convertList(csvlst)
    return cls(csvlst)

  @classmethod
  def FrompyLot (cls, src):
    return cls(src.split(u','))

  def topyLot (self):
    lst = list_to_unicode_list(self)
    for val in lst :
      check_ascii(u'value', val)
    return u','.join(lst)

  def toCSV (self):
    output = cStringIO.StringIO()
    writer = UnicodeWriter(output, dialect=DialectpyLotList())
    writer.writerow(list_to_unicode_list(list(self)))
    csvstr = output.getvalue().decode(u'utf-8')
    output.close()
    return u'[%s]' % csvstr.strip()

class StrList (BaseList):
  u"""
  StrList provides a list containing only unicode strings.
  """

  @classmethod
  def convertValue(cls, val):
    if isinstance(val, str) :
      return val.decode(ENCODING)
    else :
      return unicode(val)

class Vector (BaseList):
  u"""
  Vector provides a list containing only float values
  """

  def __repr__ (self):
    value_list = u', '.join(list_to_unicode_list(self))
    return '%s([%s])' % (self.__class__.__name__, value_list)

  @classmethod
  def convertValue(cls, val):
    return float(val)

  @classmethod
  def FromCSV (cls, src):
    src = src.strip()
    return cls(eval(src))

class pyLotList (StrList):
  u"""
  pyLotList provides a list containing only ASCII characters
  """
  @classmethod
  def convertValue(cls, val):
    val = StrList.convertValue(val)
    check_ascii(u'value', val)
    if u',' in val :
      raise ValueError, error(tr_(u'E001 - value: character "," is forbidden in pyLotList values'))
    return val

valid_datum_types = []

if __name__ == '__main__':
  import doctest
  doctest.testmod()
